﻿using Dapper;
using HrEFCore;
using HrEFCore.Models;
using HrIService;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HrService
{
	public class HFService : IHFService
	{
		private readonly MyDbContext myDbContext;

		public HFService(MyDbContext myDbContext)
        {
			this.myDbContext = myDbContext;
		}
        public async Task<int> AddHFAsync(HF hf)//人力资源添加
		{
			//myDbContext.HF.AddAsync(hf);
			//return await myDbContext.SaveChangesAsync();
			using (var conn = myDbContext.Database.GetDbConnection())
			{
				DynamicParameters dp = new DynamicParameters();
				dp.Add("@FirstKindId", hf.FirstKindId);
				dp.Add("@FirstKindName",hf.FirstKindName);
				dp.Add("@SecondKindId",hf.SecondKindId);
				dp.Add("@SecondKindName", hf.SecondKindName);
				dp.Add("@ThirdKindId", hf.ThirdKindId);
				dp.Add("@ThirdKindName", hf.ThirdKindName);
				dp.Add("@HumanName", hf.HumanName);
				dp.Add("@HumanAddress", hf.HumanAddress);
				dp.Add("@HumanPostcode", hf.HumanPostcode);
				dp.Add("@HumanProDesignation", hf.HumanProDesignation);
				dp.Add("@HumanMajorKindId", hf.HumanMajorKindId);
				dp.Add("@HumanMajorKindName", hf.HumanMajorKindName);
				dp.Add("@HumanMajorId", hf.HumanMajorId);
				dp.Add("@HunmaMajorName", hf.HunmaMajorName);
				dp.Add("@HumanTelephone", hf.HumanTelephone);
				dp.Add("@HumanMobilephone", hf.HumanMobilephone);
				dp.Add("@HumanBank", hf.HumanBank);
				dp.Add("@HumanAccount",hf.HumanAccount);
				dp.Add("@HumanQQ", hf.HumanQQ);
				dp.Add("@HumanEmail", hf.HumanEmail);
				dp.Add("@HumanHobby", hf.HumanHobby);
				dp.Add("@HumanSpeciality", hf.HumanSpeciality);
				dp.Add("@HumanSex", hf.HumanSex);
				dp.Add("@HumanReligion", hf.HumanReligion);
				dp.Add("@HumanParty", hf.HumanParty);
				dp.Add("@HumanNationality", hf.HumanNationality);
				dp.Add("@HumanRace", hf.HumanRace);
				dp.Add("@HumanBirthday", hf.HumanBirthday);
				dp.Add("@HumanBirthplace", hf.HumanBirthplace);
				dp.Add("@HumanAge", hf.HumanAge);
				dp.Add("@HumanEducatedDegree", hf.HumanEducatedDegree);
				dp.Add("@HumanEducatedYears", hf.HumanEducatedYears);
				dp.Add("@HumanEducatedMajor", hf.HumanEducatedMajor);
				dp.Add("@HumanSocietySecurityId", hf.HumanSocietySecurityId);
				dp.Add("@HumanIdCard", hf.HumanIdCard);
				dp.Add("@Remark", hf.Remark);
				dp.Add("@SalaryStandardId", hf.SalaryStandardId);
				dp.Add("@SalaryStandardName", hf.SalaryStandardName);
				dp.Add("@HumanHistroyRecords", hf.HumanHistroyRecords);
				dp.Add("@HumanFamilyMembership", hf.HumanFamilyMembership);
				dp.Add("@HumanPicture", hf.HumanPicture);
				dp.Add("@Register", hf.Register);
				dp.Add("@RegistTime", hf.RegistTime);
				dp.Add("@SalarySum",hf.SalarySum);
				string sql = "exec [dbo].[HFAdd2] @FirstKindId,@FirstKindName,@SecondKindId,@SecondKindName,\r\n\t@ThirdKindId,@ThirdKindName,@HumanName,@HumanAddress,@HumanPostcode,@HumanProDesignation,\r\n\t@HumanMajorKindId,@HumanMajorKindName,@HumanMajorId,@HunmaMajorName,@HumanTelephone,\r\n\t@HumanMobilephone,@HumanBank,@HumanAccount,@HumanQQ,@HumanEmail,@HumanHobby,@HumanSpeciality,@HumanSex,\r\n\t@HumanReligion,@HumanParty,@HumanNationality,@HumanRace,@HumanBirthday,@HumanBirthplace,@HumanAge,\r\n\t@HumanEducatedDegree,@HumanEducatedYears,@HumanEducatedMajor,@HumanSocietySecurityId,@HumanIdCard,\r\n\t@Remark,@SalaryStandardId,@SalaryStandardName,@HumanHistroyRecords,@HumanFamilyMembership,\r\n\t@HumanPicture,@Register,@RegistTime,@SalarySum";
				int row= conn.Execute(sql,dp);
				if(row > 0)
				{
					return row;
				}
				else
				{
					return row;
				}
			}
		}

		public async Task<int> BJDelete(short HufId)//标记删除
		{
			var hf = await myDbContext.HF.FindAsync(HufId);
			if (hf == null)
			{
				return 0;
			}
			hf.HumanFileStatus = false;
			hf.DeleteTime = DateTime.Now;
			myDbContext.Update(hf);
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<int> BJDeleteHF(short id)//标记删除恢复
		{
			var hf = await myDbContext.HF.FindAsync(id);
			if (hf == null)
			{
				return 0;
			}
			hf.HumanFileStatus = true;
			hf.RecoveryTime = DateTime.Now;
			myDbContext.Update(hf);
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<CFTK> cftkById(string id)//三级机构下拉
		{
			return await myDbContext.CFTK.Where(e => e.ThirdKindId == id).FirstOrDefaultAsync();
		}

		public async Task<int> Delete(short id)
		{
			var hf = await myDbContext.HF.FindAsync(id);
			if(hf == null)
			{
				return 0;
			}
			myDbContext.Remove(hf);
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<int> FuHeTGHF(HF hf)//复核通过
		{
			//myDbContext.HF.Where(e=>e.CheckStatus == 1 && e.Checker == hf.Checker && e.CheckTime == DateTime.Now);
			//myDbContext.HF.Update(hf);
			//return await myDbContext.SaveChangesAsync();

			var entity = myDbContext.HF.Attach(hf);
			entity.Property(e => e.CheckStatus).IsModified = true;
			entity.Property(e => e.Checker).IsModified = true;
			entity.Property(e => e.CheckTime).IsModified = true;
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<FenYe2<HF>> SelectAllFYHFAsync(FenYe2<HF> fenYe)
		{
			//string conStr = "Data Source=LAPTOP-2ODIGOV0\\MSSQLSERVER2;Initial Catalog=HR_DB;Integrated Security=True;";
			using (var conn= myDbContext.Database.GetDbConnection())
			{
				DynamicParameters dp = new DynamicParameters();
				dp.Add("@currentPage",fenYe.CurrentPage);
				dp.Add("@pageSize",fenYe.PageSize);
				dp.Add("@keyName", "HufId");
				dp.Add("@tableName","HF");
				dp.Add("@rows",direction:ParameterDirection.Output,dbType:DbType.Int32);
				string sql = "exec [dbo].[proc_FenYe] @currentPage,@pageSize,@keyName,@tableName,@rows out";
				fenYe.List = await conn.QueryAsync<HF>(sql,dp);
				fenYe.Rows = dp.Get<int>("rows");
				return fenYe;
			}
		}

		public async Task<CM> SelectCMById(string id)//职位分类名称
		{
			return await myDbContext.CM.FirstOrDefaultAsync(e=>e.MajorId == id);
		}

		public async Task<CMK> SelectCMKById(string id)//职位名称
		{
			return await myDbContext.CMK.FirstOrDefaultAsync(e=>e.MajorKindId == id);
		}

		public async Task<FenYe2<HF>> SelectFYTJHF(int CurrentPage, int PageSize, string where)//条件分页查询
		{
			using(var conn = myDbContext.Database.GetDbConnection())
			{
				DynamicParameters dp = new DynamicParameters();
				dp.Add("@pageSize", PageSize);
				dp.Add("@keyName", "HufId");
				dp.Add("@tableName", "HF");
				dp.Add("@where", where);
				dp.Add("@currentPage",CurrentPage);
				dp.Add("@lie","*");
				dp.Add("@rows", direction: ParameterDirection.Output, dbType: DbType.Int32);
				string sql = "exec [dbo].[proc_FenYe2] @pageSize,@keyName,@tableName,@where,@currentPage,@lie,@rows out";
				IEnumerable<HF> list = await conn.QueryAsync<HF>(sql,dp);
				int rows = dp.Get<int>("rows");
				FenYe2<HF> fy=new FenYe2<HF>()
				{
					List=list,
					Rows=rows
				};
				return fy;
			}
		}

		public async Task<HF> SelectHFById(short id)
		{
			return await myDbContext.HF.FirstOrDefaultAsync(e=>e.HufId == id);
		}

		public async Task<List<CM>> SelectNameLD(string id)
		{
			return await myDbContext.CM.Where(e => e.MajorKindId == id).ToListAsync();
		}

		public Task<List<HF>> SelectSouSuoHF(string name)//关键字搜索
		{
			return myDbContext.HF.Where(e=>e.HumanName.Contains(name)).ToListAsync();		 
		}

		public async Task<SS> SelectSSById(string id)//薪酬
		{
			return await myDbContext.SS.FirstOrDefaultAsync(e=>e.StandardId == id);
		}

		public async Task<SSD> SelectSSDById(string id)//薪酬
		{
			return await myDbContext.SSD.FirstOrDefaultAsync(e=>e.StandardId == id);
		}

		public async Task<List<SSD>> SSDXlAsync()
		{
			return await myDbContext.SSD.ToListAsync();
		}

		public async Task<List<SS>> SSXLAsync()//ss表
		{
			return await myDbContext.SS.ToListAsync();
		}

		public async Task<int> Update(HF hf)
		{
			var entity = myDbContext.HF.Attach(hf);
			entity.Property(e=>e.HumanName).IsModified = true;
			entity.Property(e => e.HumanSex).IsModified = true;
			entity.Property(e => e.HumanEmail).IsModified = true;
			entity.Property(e => e.HumanMobilephone).IsModified = true;
			entity.Property(e => e.HumanTelephone).IsModified = true;
			entity.Property(e => e.HumanQQ).IsModified = true;
			entity.Property(e => e.HumanAddress).IsModified = true;//地址
			entity.Property(e => e.HumanNationality).IsModified = true;//国籍
			entity.Property(e => e.HumanBirthplace).IsModified = true;//出生地
			entity.Property(e => e.HumanBirthday).IsModified = true;//生日
			entity.Property(e => e.HumanRace).IsModified = true;//民族
			entity.Property(e => e.HumanReligion).IsModified = true;//宗教信仰
			entity.Property(e => e.HumanParty).IsModified = true;//政治面貌
			entity.Property(e => e.HumanIdCard).IsModified = true;//身份证号
			entity.Property(e => e.HumanSocietySecurityId).IsModified = true;//社会保障号
			entity.Property(e => e.HumanAge).IsModified = true;
			entity.Property(e => e.HumanEducatedDegree).IsModified = true;//学历
			entity.Property(e => e.HumanEducatedYears).IsModified = true;//教育年限
			entity.Property(e => e.HumanEducatedMajor).IsModified = true;//学历专业
			entity.Property(e => e.SalaryStandardId).IsModified = true;//薪酬标准编号
			entity.Property(e => e.SalaryStandardName).IsModified = true;//薪酬标准名称
			entity.Property(e => e.HumanBank).IsModified = true;//开户行
			entity.Property(e => e.HumanAccount).IsModified = true;//银行账号
			entity.Property(e => e.Changer).IsModified = true;//变更人
			entity.Property(e => e.ChangeTime).IsModified = true;//变更时间
			entity.Property(e => e.HumanSpeciality).IsModified = true;// 特长
			entity.Property(e => e.HumanHobby).IsModified = true;//爱好
			entity.Property(e => e.HumanHistroyRecords).IsModified = true;//个人履历
			entity.Property(e => e.HumanFamilyMembership).IsModified = true;//家庭关系信息
			entity.Property(e => e.Remark).IsModified = true;//备注
			entity.Property(e => e.FileChangAmount).IsModified = true;//档案累计变更次数
			entity.Property(e => e.LastlyChangeTime).IsModified = true;//档案最近更改时间
			entity.Property(e => e.CheckStatus).IsModified = true;//复核状态要改变
			return await myDbContext.SaveChangesAsync();
		}
	}
}
